/*
 * Copyright (C) 2020 Jimmy Hogoboom
 *
 * This file is part of Limitlist.
 *
 * Limitlist is free software: you can redistribute it and/or modify
 * it under the terms of the Affero GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Limitlist is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Affero GNU General Public License for more details.
 *
 * You should have received a copy of the Affero GNU General Public License
 * along with Limitlist.  If not, see <https://www.gnu.org/licenses/>.
 */

// SPDX-License-Identifier: AGPL-3.0-or-later

import React from 'react';
import { render } from '@testing-library/react';
import ProductList from './ProductList';
import { IProductListItem } from 'core/types/IProductListItem';

let testList = [
  {
    id: 'testItemId1',
    name: 'Test Name',
    price: 1.99,
    quantity: 1,
  } as IProductListItem,
  {
    id: 'testItemId2',
    name: 'Second Test Name',
    price: 4,
    quantity: 2,
  } as IProductListItem,
];

test('renders empty list message', () => {
  const { getByText } = render(<ProductList />);
  const textElement = getByText(/Your list is empty./i);
  expect(textElement).toBeInTheDocument();
});

test('renders list of items', () => {
  const { getAllByText } = render(
    <ProductList
      items={testList}
    />
  );
  const listItems = getAllByText('Test Name', { exact: false });
  expect(listItems.length).toBe(testList.length);
});

test('calculates grand total', () => {
  // test empty list
  const { getByTitle, rerender } = render(<ProductList items={[]} />);
  const totalBefore = getByTitle('Grand Total');
  expect(totalBefore).toHaveTextContent('$0.00');

  // test list with items
  rerender(<ProductList items={testList} />);

  const totalAfter = getByTitle('Grand Total');
  expect(totalAfter).toHaveTextContent('$9.99');
});

// TODO:
// test('sorting items calls moveItem', () => {});

// TODO:
// test('clicking Add Item calls addItem', () => {});

// TODO: test update, delete somehow
